package com.ruoyi.web.controller.gcgl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUpMoneyUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcQttkqrd;
import com.ruoyi.gcgl.domain.GcQttkqrditem;
import com.ruoyi.gcgl.domain.GcSupply;
import com.ruoyi.gcgl.mapper.GcSendTipMapper;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcQttkqrdService;
import com.ruoyi.gcgl.service.IGcQttkqrditemService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.gcgl.service.IGcSupplyService;
import com.ruoyi.web.controller.tool.AliyunSmsUtils;

/**
 * 日常支付Controller
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Controller
@RequestMapping("/gcgl/qttkqrd")
public class GcQttkqrdController extends BaseController
{
    private String prefix = "gcgl/qttkqrd";

    @Autowired
    private IGcQttkqrdService gcQttkqrdService;
    @Autowired
    private IGcQttkqrditemService gcQttkqrditemService;
    @Autowired
    private IGcSupplyService gcSupplyService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private GcSendTipMapper gcSendTipMapper;

    @RequiresPermissions("gcgl:qttkqrd:view")
    @GetMapping()
    public String qttkqrd(ModelMap mmap)
    {
        return prefix + "/qttkqrd";
    }

    /**
     * 查询日常支付列表
     */
    @RequiresPermissions("gcgl:qttkqrd:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcQttkqrd gcQttkqrd)
    {
        startPage();
        gcQttkqrd.setDeleteFlag(1);
        if(getSysUser().isShow43R(getUserId())){
        gcQttkqrd.setCreateUser(getLoginName());
        }
        List<GcQttkqrd> list = gcQttkqrdService.selectGcQttkqrdList(gcQttkqrd);
        return getDataTable(list);
    }

    /**
     * 导出日常支付列表
     */
    @RequiresPermissions("gcgl:qttkqrd:export")
    @Log(title = "日常支付", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcQttkqrd gcQttkqrd)
    {
    	gcQttkqrd.setDeleteFlag(1);
    	if(getSysUser().isShow()){
            gcQttkqrd.setCreateUser(getLoginName());
            }
        List<GcQttkqrd> list = gcQttkqrdService.selectGcQttkqrdList(gcQttkqrd);
        ExcelUtil<GcQttkqrd> util = new ExcelUtil<GcQttkqrd>(GcQttkqrd.class);
        return util.exportExcel(list, "日常支付数据");
    }

    /**
     * 新增日常支付
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	mmap.put("tkqrr", getLoginName());
    	
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	gcSupply.setType(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("gcSupplylis", gcSupplylis);
    	
        return prefix + "/add";
    }

    /**
     * 新增保存日常支付
     */
    @RequiresPermissions("gcgl:qttkqrd:add")
    @Log(title = "日常支付", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcQttkqrd gcQttkqrd)
    {
    	try {
    		SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
    		SimpleDateFormat sdm = new SimpleDateFormat("MM");
    		int year = Integer.parseInt(sdy.format(new Date()));
    		int month = Integer.parseInt(sdm.format(new Date()));
    		GcQttkqrd newObj = new GcQttkqrd();
    		newObj.setDeleteFlag(1);
    		newObj.setCreateAt(DateUtils.getFisrtDayOfMonth(year, month));
    		newObj.setUpdateAt(DateUtils.getLastDayOfMonth(year, month));
    		List<GcQttkqrd> lisCount = gcQttkqrdService.selectGcQttkqrdList(newObj);
    		
    		GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	gcSupply.setType(1);
        	gcSupply.setName(gcQttkqrd.getTkdw());
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    		if(gcSupplylis!=null && gcSupplylis.size()>0){
    			gcQttkqrd.setTkdw(gcSupplylis.get(0).getName());
    			gcQttkqrd.setTkzh(gcSupplylis.get(0).getSkzh());
    			gcQttkqrd.setNssbh(gcSupplylis.get(0).getNssbh());
    			gcQttkqrd.setKhyh(gcSupplylis.get(0).getKhyh());
    		}
    		
    		String no = String.format("%02d", lisCount.size()+1);
    		String l_month = String.format("%02d", month);
    		gcQttkqrd.setOrderNo(year+"-"+l_month+"-"+no);
    		gcQttkqrd.setDeleteFlag(1);
    		gcQttkqrd.setCreateAt(new Date());
    		gcQttkqrd.setUpdateAt(new Date());
    		gcQttkqrd.setCreateUser(getLoginName());
    		gcQttkqrd.setUpdateUser(getLoginName());
    		gcQttkqrd.setStatus(0);
    		gcQttkqrd.setSqr(getLoginName());
    		gcQttkqrd.setSqrAt(new Date());
    		
//    		GcProject gcProject = gcProjectService.selectGcProjectById(Long.parseLong(gcQttkqrd.getGcProjectId().toString()));
//    		gcQttkqrd.setGcProjectName(gcProject.getName());
    		
    		gcQttkqrdService.insertGcQttkqrd(gcQttkqrd);
    		
    		Double hejexx = 0.0;
        	for(GcQttkqrditem obj:gcQttkqrd.getItem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(obj.getJine().toString()));
        		obj.setDeleteFlag(1);
        		obj.setCreateAt(new Date());
        		obj.setUpdateAt(new Date());
        		obj.setCreateUser(getLoginName());
        		obj.setUpdateUser(getLoginName());
        		obj.setQttkqrdId(gcQttkqrd.getId());
        		gcQttkqrditemService.insertGcQttkqrditem(obj);
        	}
        	
        	DecimalFormat mf = new DecimalFormat();
			mf.applyPattern("#0.00");
			gcQttkqrd.setHejexx(mf.format(hejexx));
        	gcQttkqrd.setHeje(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcQttkqrdService.updateGcQttkqrd(gcQttkqrd);
        	
        	//发送消息通知
//        	gcSendTipService.sendTip("qttk", "日常支付", gcQttkqrd.getOrderNo(), 
//        			gcQttkqrd.getCreateUser(), DateUtils.dateTime(gcQttkqrd.getCreateAt()), 
//        			gcQttkqrd.getStatus().toString(), null, gcQttkqrd.getId().toString());
        	
        	try {
//        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQttkqrd.getCreateUser());
//        		if(userlis!=null && userlis.size()>0){
//        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        		String phonenumber = caiw;
        			AliyunSmsUtils.returnSmsTz(phonenumber);
//        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
        return toAjax(1);
    }

    /**
     * 修改日常支付
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcQttkqrd gcQttkqrd = gcQttkqrdService.selectGcQttkqrdById(id);
        GcQttkqrditem gcQttkqrditem = new GcQttkqrditem();
        gcQttkqrditem.setQttkqrdId(id);
        List<GcQttkqrditem> items = gcQttkqrditemService.selectGcQttkqrditemList(gcQttkqrditem);
        gcQttkqrd.setItem(items);
        mmap.put("gcQttkqrd", gcQttkqrd);
        
        GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	gcSupply.setType(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("gcSupplylis", gcSupplylis);
    	
        return prefix + "/edit";
    }

    /**
     * 修改保存日常支付
     */
    @Log(title = "日常支付", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcQttkqrd gcQttkqrd)
    {
    	try {
    		gcQttkqrditemService.deleteGcQttkqrditemById(gcQttkqrd.getId());
    		
    		Double hejexx = 0.0;
        	//item
        	for(GcQttkqrditem item : gcQttkqrd.getItem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(item.getJine().toString()));
        		
        		item.setDeleteFlag(1);
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setQttkqrdId(gcQttkqrd.getId());
        		gcQttkqrditemService.insertGcQttkqrditem(item);
        	}
        	
        	GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	gcSupply.setType(1);
        	gcSupply.setName(gcQttkqrd.getTkdw());
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    		if(gcSupplylis!=null && gcSupplylis.size()>0){
    			gcQttkqrd.setTkdw(gcSupplylis.get(0).getName());
    			gcQttkqrd.setTkzh(gcSupplylis.get(0).getSkzh());
    			gcQttkqrd.setNssbh(gcSupplylis.get(0).getNssbh());
    			gcQttkqrd.setKhyh(gcSupplylis.get(0).getKhyh());
    		}
        	
    		DecimalFormat mf = new DecimalFormat();
			mf.applyPattern("#0.00");
			gcQttkqrd.setHejexx(mf.format(hejexx));
        	gcQttkqrd.setHeje(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcQttkqrd.setUpdateAt(new Date());
        	gcQttkqrd.setUpdateUser(getLoginName());
        	
//        	GcProject gcProject = gcProjectService.selectGcProjectById(Long.parseLong(gcQttkqrd.getGcProjectId().toString()));
//    		gcQttkqrd.setGcProjectName(gcProject.getName());
        	
        	gcQttkqrdService.updateGcQttkqrd(gcQttkqrd);
        	
        	try {
//        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQttkqrd.getCreateUser());
//        		if(userlis!=null && userlis.size()>0){
//        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        		String phonenumber = caiw;
        			AliyunSmsUtils.returnSmsTz(phonenumber);
//        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}        	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
    	
        return toAjax(1);
    }

    /**
     * 删除日常支付
     */
    @RequiresPermissions("gcgl:qttkqrd:remove")
    @Log(title = "日常支付", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcQttkqrdService.deleteGcQttkqrdByIds(ids));
    }
    
    /////财务审批
    @RequiresPermissions("gcgl:qttkqrd:cwpsview")
    @GetMapping("/cwps")
    public String cwps()
    {
        return prefix + "/qttkqrdcwps";
    }
    
    @PostMapping("/cwpslist")
    @ResponseBody
    public TableDataInfo cwpslist(GcQttkqrd gcQttkqrd)
    {
        startPage();
        gcQttkqrd.setDeleteFlag(1);
        gcQttkqrd.setStatus(1);
        List<GcQttkqrd> list = gcQttkqrdService.selectGcQttkqrdList(gcQttkqrd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwyes")
    @ResponseBody
    public AjaxResult cwyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQttkqrd obj = new GcQttkqrd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(3);
    		obj.setCwsh(getLoginName());
    		obj.setCwshAt(new Date());
    		gcQttkqrdService.updateGcQttkqrd(obj);
    		
    		GcQttkqrd gcQttkqrd = gcQttkqrdService.selectGcQttkqrdById(Long.parseLong(id[i]));
    		//发送消息通知
//        	gcSendTipService.sendTip("qttk", "日常支付", gcQttkqrd.getOrderNo(), 
//        			gcQttkqrd.getCreateUser(), DateUtils.dateTime(gcQttkqrd.getCreateAt()), 
//        			gcQttkqrd.getStatus().toString(), null, gcQttkqrd.getId().toString());
    		
    		try {
//        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQttkqrd.getCreateUser());
//        		if(userlis!=null && userlis.size()>0){
//        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
    			String phonenumber = zjl;
        			AliyunSmsUtils.returnSmsTz(phonenumber);
//        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwno")
    @ResponseBody
    public AjaxResult cwno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQttkqrd obj = new GcQttkqrd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(4);
    		obj.setCwsh(getLoginName());
    		obj.setCwshAt(new Date());
    		gcQttkqrdService.updateGcQttkqrd(obj);
    		
    		GcQttkqrd gcQttkqrd = gcQttkqrdService.selectGcQttkqrdById(Long.parseLong(id[i]));
    		//发送消息通知
//        	gcSendTipService.sendTip("qttk", "日常支付", gcQttkqrd.getOrderNo(), 
//        			gcQttkqrd.getCreateUser(), DateUtils.dateTime(gcQttkqrd.getCreateAt()), 
//        			gcQttkqrd.getStatus().toString(), null, gcQttkqrd.getId().toString());
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQttkqrd.getCreateUser());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	return toAjax(1);
    }
    
    /////总经理审批
    @RequiresPermissions("gcgl:qttkqrd:zjlpsview")
    @GetMapping("/zjlps")
    public String zjlps()
    {
        return prefix + "/qttkqrdzjlps";
    }
    
    @PostMapping("/zjlpslist")
    @ResponseBody
    public TableDataInfo zjlpslist(GcQttkqrd gcQttkqrd)
    {
        startPage();
        gcQttkqrd.setDeleteFlag(1);
        gcQttkqrd.setStatus(0);
        List<GcQttkqrd> list = gcQttkqrdService.selectGcQttkqrdList(gcQttkqrd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQttkqrd obj = new GcQttkqrd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(1);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcQttkqrdService.updateGcQttkqrd(obj);
    		
    		GcQttkqrd gcQttkqrd = gcQttkqrdService.selectGcQttkqrdById(Long.parseLong(id[i]));
    		//发送消息通知
//        	gcSendTipService.sendTip("qttk", "日常支付", gcQttkqrd.getOrderNo(), 
//        			gcQttkqrd.getCreateUser(), DateUtils.dateTime(gcQttkqrd.getCreateAt()), 
//        			gcQttkqrd.getStatus().toString(), null, gcQttkqrd.getId().toString());
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQttkqrd.getCreateUser());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQttkqrd obj = new GcQttkqrd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(2);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcQttkqrdService.updateGcQttkqrd(obj);
    		
    		GcQttkqrd gcQttkqrd = gcQttkqrdService.selectGcQttkqrdById(Long.parseLong(id[i]));
    		//发送消息通知
//        	gcSendTipService.sendTip("qttk", "日常支付", gcQttkqrd.getOrderNo(), 
//        			gcQttkqrd.getCreateUser(), DateUtils.dateTime(gcQttkqrd.getCreateAt()), 
//        			gcQttkqrd.getStatus().toString(), null, gcQttkqrd.getId().toString());
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQttkqrd.getCreateUser());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	return toAjax(1);
    }
    
    @GetMapping("/qttkqrdprint/{id}")
    public String qttkqrdprint(@PathVariable("id") Long id, ModelMap mmap){
    	GcQttkqrd obj = gcQttkqrdService.selectGcQttkqrdById(id);
        
    	GcQttkqrditem items = new GcQttkqrditem();
        items.setQttkqrdId(id);
        List<GcQttkqrditem> itemlis= gcQttkqrditemService.selectGcQttkqrditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/qttkqrdprint";
    }
    
    @GetMapping("/qttkqrdcwsp/{id}")
    public String qttkqrdcwsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcQttkqrd obj = gcQttkqrdService.selectGcQttkqrdById(id);
        
    	GcQttkqrditem items = new GcQttkqrditem();
        items.setQttkqrdId(id);
        List<GcQttkqrditem> itemlis= gcQttkqrditemService.selectGcQttkqrditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/qttkqrdcwsp";
    }
    
    @GetMapping("/qttkqrdzjlsp/{id}")
    public String qttkqrdzjlsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcQttkqrd obj = gcQttkqrdService.selectGcQttkqrdById(id);
        
    	GcQttkqrditem items = new GcQttkqrditem();
        items.setQttkqrdId(id);
        List<GcQttkqrditem> itemlis= gcQttkqrditemService.selectGcQttkqrditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/qttkqrdzjlsp";
    }
}